package logic

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/pkg/errors"

	"zero-demo-rpc/mall/Register/api/internal/svc"
	"zero-demo-rpc/mall/Register/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.RegisterReq) (resp *types.RegisterReply, err error) {
	// todo: add your logic here and delete this line

	print("接收登录请求：", req.UserName)

	db, err := sql.Open("mysql", "root:huohuo@tcp(127.0.0.1:3306)/test_2?charset=utf8")

	//root为数据库用户名，后面为密码，tcp代表tcp协议，test处填写自己的数据库名称
	rows, err := db.Query("SELECT * FROM user where user_name=?", req.UserName)

	//print("rows:", rows, "err:", err)

	if err != nil {
		return nil, errors.New("")
	} else {
		var s info

		for rows.Next() {
			err := rows.Scan(&s.name, &s.address, &s.signcrt, &s.signkey, &s.tlscrt, &s.tlskey, &s.productsum)
			//err := rows.Scan(&s.name, &s.address)
			fmt.Println(err)
		}

		if s.name != "" {
			print("登录成功")
			return &types.RegisterReply{
				UserAddress: s.address,
			}, nil
		} else {
			print("登陆失败:", s.name)
			return nil, errors.New("")
		}

		//如果存在用户，则返回用户的地址

	}

}
